package adminServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swim2.sessionbeans.*;

public class AdminPanelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public AdminPanelServlet() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		
		try{
			Context ctx = getInitialContext();			
			AdminSystemRemote adminSystemRemote = (AdminSystemRemote) ctx.lookup("adminJNDI");
			String mail = request.getAttribute("mail").toString();
			String nameAndSurname = adminSystemRemote.getUserNameAndSurname(mail);
			int id = (Integer) request.getAttribute("id");
			String password = request.getAttribute("password").toString();
			int newRequests=0;
			if(adminSystemRemote.getNewAbilityRequests().size()!=0){
				newRequests=1;
			}
			request.setAttribute("nameAndSurname", nameAndSurname);
			request.setAttribute("newRequests", newRequests);
			request.setAttribute("id", id);
			request.setAttribute("mail", mail);
			request.setAttribute("password", password);
			RequestDispatcher dispatcher =
			request.getRequestDispatcher("/AdminJsp/AdminPanel.jsp");
			dispatcher.forward(request, response);	
		}
		catch (Exception e) {
			e.printStackTrace();
			out.print("Admin panel exception");
		}
	}

	static public Context getInitialContext() throws Exception{
		Hashtable<String,String> env = new Hashtable<String,String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL, "localhost:1099");
		return new InitialContext(env);
	}
}
